using System;
using System.ComponentModel;
using System.Windows.Forms;
using Ardal.MSBuildShellExtension.DomainModel.API;

/*
 * Copyright (c) 2007-2008, Thomas Ardal (thomasardalatgmail.com)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of Thomas Ardal nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
namespace Ardal.MSBuildShellExtension.Configurator
{
    public partial class TargetControl : UserControl
    {
        private ITarget currentSelected;

        public TargetControl()
        {
            InitializeComponent();

            foreach (ITarget target in DomainModelFacade.Instance.Targets)
            {
                targetsListBox.Items.Add(target);
            }

            verbosityComboBox.Items.AddRange(MSBuildConvertions.GetAllVerbosities());
            configurationComboBox.Items.AddRange(MSBuildConvertions.GetAllConfigurations());

            targetsListBox.Focus();
            if (targetsListBox.Items.Count > 0)
            {
                targetsListBox.SelectedIndex = 0;
            }
        }

        private void contextMenuItemsListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            ITarget item = (ITarget)targetsListBox.SelectedItem;
            if (item != null)
            {
                titleTextBox.Text = item.Title;
                targetTextBox.Text = item.MSBuildTarget;
                showErrorListCheckBox.Checked = item.ShowErrorList;
                verbosityComboBox.SelectedItem = item.Verbosity;
                configurationComboBox.SelectedItem = item.Configuration;
                currentSelected = item;
            }
        }

        private void targetTextBox_Validated(object sender, EventArgs e)
        {
            if (currentSelected != null)
            {
                if (!targetTextBox.Text.Equals(currentSelected.MSBuildTarget))
                {
                    currentSelected.MSBuildTarget = targetTextBox.Text;
                }
            }
        }

        private void showErrorListCheckBox_Validated(object sender, EventArgs e)
        {
            if (currentSelected != null)
            {
                if (showErrorListCheckBox.Checked != currentSelected.ShowErrorList)
                {
                    currentSelected.ShowErrorList = showErrorListCheckBox.Checked;
                }
            }
        }

        private void verbosityComboBox_Validating(object sender, CancelEventArgs e)
        {
            if (verbosityComboBox.SelectedItem == null)
            {
                MessageBox.Show("Not a valid verbosity level.");
                e.Cancel = true;
            }
        }

        private void verbosityComboBox_Validated(object sender, EventArgs e)
        {
            if (currentSelected != null)
            {
                //Only update verbosity, if it is different than current verbosity
                if (!verbosityComboBox.SelectedItem.Equals(currentSelected.Verbosity))
                {
                    currentSelected.Verbosity = (Verbosity)verbosityComboBox.SelectedItem;
                }
            }
        }

        private void configurationComboBox_Validated(object sender, EventArgs e)
        {
            if (currentSelected != null)
            {
                //Only update verbosity, if it is different than current verbosity
                if (!configurationComboBox.SelectedItem.Equals(currentSelected.Configuration))
                {
                    currentSelected.Configuration = (Configuration)configurationComboBox.SelectedItem;
                }
            }
        }

        private void configurationComboBox_Validating(object sender, CancelEventArgs e)
        {
            if (configurationComboBox.SelectedItem == null)
            {
                MessageBox.Show("Not a valid configuration.");
                e.Cancel = true;
            }
        }

        private void removeButton_Click(object sender, EventArgs e)
        {
            if (targetsListBox.SelectedIndex != -1)
            {
                ITarget deleteableItem = (ITarget)targetsListBox.Items[targetsListBox.SelectedIndex];
                deleteableItem.Deleted = true;
                targetsListBox.Items.Remove(deleteableItem);
                //When removing a target, also check if any extensions are referencing the deleted target.
                foreach (IExtension extension in DomainModelFacade.Instance.Extensions)
                {
                    if (extension.Targets.Contains(deleteableItem))
                    {
                        extension.ToDeleteTargets.Add(deleteableItem);
                        extension.Targets.Remove(deleteableItem);
                    }
                }
                StateController.ChangeState(new RemoveTargetState(this));
            }
        }

        private void newButton_Click(object sender, EventArgs e)
        {
            currentSelected = null;
            StateController.ChangeState(new NewContextMenuItemState(this));
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(titleTextBox.Text))
            {
                errorProvider1.SetError(titleTextBox, "Title cannot be empty.");
                titleTextBox.Focus();
                return;
            }
            errorProvider1.SetError(titleTextBox, "");
            if (string.IsNullOrEmpty(targetTextBox.Text))
            {
                errorProvider1.SetError(targetTextBox, "Target cannot be empty.");
                targetTextBox.Focus();
                return;
            }
            errorProvider1.SetError(targetTextBox, "");
            if (string.IsNullOrEmpty(verbosityComboBox.Text))
            {
                errorProvider1.SetError(verbosityComboBox, "Verbosity cannot be empty.");
                verbosityComboBox.Focus();
                return;
            }
            errorProvider1.SetError(verbosityComboBox, "");
            if (string.IsNullOrEmpty(configurationComboBox.Text))
            {
                errorProvider1.SetError(configurationComboBox, "Configuration cannot be empty.");
            }
            errorProvider1.SetError(configurationComboBox, "");
            StateController.ChangeState(new AddContextMenuItemState(this));
            ITarget newItem = DomainModelFacade.Instance.AddTarget(titleTextBox.Text, targetTextBox.Text, (Verbosity)verbosityComboBox.SelectedItem, (Configuration)configurationComboBox.SelectedItem, showErrorListCheckBox.Checked);
            newItem.Modified = true;
            targetsListBox.Items.Add(newItem);
            targetsListBox.SelectedItem = newItem;
        }

        #region States
        private class RemoveTargetState : IState
        {
            private readonly TargetControl targetControl;

            public RemoveTargetState(TargetControl targetControl)
            {
                this.targetControl = targetControl;
            }

            #region IState Members

            public void Activate()
            {
                if (targetControl.targetsListBox.Items.Count > 0)
                {
                    targetControl.targetsListBox.SelectedItem = targetControl.targetsListBox.Items[0];
                }
                else
                {
                    targetControl.titleTextBox.Text = "";
                    targetControl.titleTextBox.Enabled = false;
                    targetControl.targetTextBox.Text = "";
                    targetControl.targetTextBox.Enabled = false;
                    targetControl.verbosityComboBox.Text = "";
                    targetControl.verbosityComboBox.Enabled = false;
                    targetControl.configurationComboBox.Text = "";
                    targetControl.configurationComboBox.Enabled = false;
                    targetControl.showErrorListCheckBox.Checked = false;
                    targetControl.showErrorListCheckBox.Enabled = false;
                    targetControl.addButton.Enabled = false;
                    targetControl.removeButton.Enabled = false;
                }
            }

            #endregion
        }

        private class AddContextMenuItemState : IState
        {
            private readonly TargetControl targetControl;

            public AddContextMenuItemState(TargetControl targetControl)
            {
                this.targetControl = targetControl;
            }

            #region IState Members

            public void Activate()
            {
                targetControl.titleTextBox.Enabled = false;
                targetControl.newButton.Enabled = true;
                targetControl.removeButton.Enabled = true;
                targetControl.addButton.Enabled = false;
            }

            #endregion
        }

        private class NewContextMenuItemState : IState
        {
            private readonly TargetControl targetControl;

            public NewContextMenuItemState(TargetControl targetControl)
            {
                this.targetControl = targetControl;
            }

            #region IState Members

            public void Activate()
            {
                targetControl.targetsListBox.SelectedIndex = -1;
                targetControl.titleTextBox.Text = "";
                targetControl.titleTextBox.Enabled = true;
                targetControl.targetTextBox.Text = "";
                targetControl.targetTextBox.Enabled = true;
                targetControl.verbosityComboBox.SelectedItem = Verbosity.Normal;
                targetControl.verbosityComboBox.Enabled = true;
                targetControl.showErrorListCheckBox.Checked = true;
                targetControl.showErrorListCheckBox.Enabled = true;
                targetControl.newButton.Enabled = false;
                targetControl.removeButton.Enabled = false;
                targetControl.addButton.Enabled = true;
                targetControl.titleTextBox.Focus();
                targetControl.configurationComboBox.Enabled = true;
                targetControl.configurationComboBox.SelectedItem = Configuration.Debug;
            }

            #endregion
        }
        #endregion

        private void verbosityComboBox_Validated_1(object sender, EventArgs e)
        {
            if (currentSelected != null)
            {
                currentSelected.Verbosity = (Verbosity)verbosityComboBox.SelectedItem;
            }
        }

        private void configurationComboBox_Validated_1(object sender, EventArgs e)
        {
            if (currentSelected != null)
            {
                currentSelected.Configuration = (Configuration)configurationComboBox.SelectedItem;
            }
        }

        private void targetTextBox_Validated_1(object sender, EventArgs e)
        {
            if (currentSelected != null)
            {
                currentSelected.MSBuildTarget = targetTextBox.Text;
            }
        }

        private void showErrorListCheckBox_Validated_1(object sender, EventArgs e)
        {
            if (currentSelected != null)
            {
                currentSelected.ShowErrorList = showErrorListCheckBox.Checked;
            }
        }
    }
}
